<template>
  <div class="app-container">
    <el-table
      v-loading="listLoading"
      :data="list"
      :header-cell-style="{'background-color': '#F5F8FB'}"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column align="center" label="序号" type="index" :index="indexMethod" />
      <el-table-column align="center" label="企业ID">
        <template slot-scope="scope">
          <span :title="scope.row.businessId">{{ scope.row.businessId }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="系统id">
        <template slot-scope="scope">
          <span :title="scope.row.systemId">{{ scope.row.systemId }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="菜单ID">
        <template slot-scope="scope">
          <span :title="scope.row.menuId">{{ scope.row.menuId }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="接口ID">
        <template slot-scope="scope">
          <span :title="scope.row.interfaceId">{{ scope.row.interfaceId }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="接口名称">
        <template slot-scope="scope">
          <span :title="scope.row.interfaceName">{{ scope.row.interfaceName }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="创建用户ID">
        <template slot-scope="scope">
          <span :title="scope.row.userId">{{ scope.row.userId }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="类型">
        <template slot-scope="scope">
          <span :title="scope.row.type">{{ scope.row.type }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="状态">
        <template slot-scope="scope">
          <span :title="scope.row.status">{{ scope.row.status }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="创建人">
        <template slot-scope="scope">
          <span :title="scope.row.createId">{{ scope.row.createId }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="创建时间">
        <template slot-scope="scope">
          <span :title="scope.row.createTime">{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="更新人">
        <template slot-scope="scope">
          <span :title="scope.row.updateId">{{ scope.row.updateId }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="更新时间">
        <template slot-scope="scope">
          <span :title="scope.row.updateTime">{{ scope.row.updateTime }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="删除状态(0-正常,1-已删除)">
        <template slot-scope="scope">
          <span :title="scope.row.delFlag">{{ scope.row.delFlag }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" align="center" label="操作" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        :current-page="listQuery.page"
        :page-sizes="[10,20,30,50]"
        :page-size="listQuery.limit"
        :total="total"
        background
        layout="total, sizes, prev, pager, next"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
    <el-dialog title="interface" :visible.sync="dialogVisible">
      <el-form ref="interfaceForm" :model="interface" :rules="rules" size="mini">
        <el-form-item label="企业ID" prop="businessId" :label-width="formLabelWidth">
          <el-input v-model="interface.businessId" autocomplete="off" />
        </el-form-item>
        <el-form-item label="系统id" prop="systemId" :label-width="formLabelWidth">
          <el-input v-model="interface.systemId" autocomplete="off" />
        </el-form-item>
        <el-form-item label="菜单ID" prop="menuId" :label-width="formLabelWidth">
          <el-input v-model="interface.menuId" autocomplete="off" />
        </el-form-item>
        <el-form-item label="接口ID" prop="interfaceId" :label-width="formLabelWidth">
          <el-input v-model="interface.interfaceId" autocomplete="off" />
        </el-form-item>
        <el-form-item label="接口名称" prop="interfaceName" :label-width="formLabelWidth">
          <el-input v-model="interface.interfaceName" autocomplete="off" />
        </el-form-item>
        <el-form-item label="创建用户ID" prop="userId" :label-width="formLabelWidth">
          <el-input v-model="interface.userId" autocomplete="off" />
        </el-form-item>
        <el-form-item label="类型" prop="type" :label-width="formLabelWidth">
          <el-input v-model="interface.type" autocomplete="off" />
        </el-form-item>
        <el-form-item label="状态" prop="status" :label-width="formLabelWidth">
          <el-input v-model="interface.status" autocomplete="off" />
        </el-form-item>
        <el-form-item label="创建人" prop="createId" :label-width="formLabelWidth">
          <el-input v-model="interface.createId" autocomplete="off" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createTime" :label-width="formLabelWidth">
          <el-input v-model="interface.createTime" autocomplete="off" />
        </el-form-item>
        <el-form-item label="更新人" prop="updateId" :label-width="formLabelWidth">
          <el-input v-model="interface.updateId" autocomplete="off" />
        </el-form-item>
        <el-form-item label="更新时间" prop="updateTime" :label-width="formLabelWidth">
          <el-input v-model="interface.updateTime" autocomplete="off" />
        </el-form-item>
        <el-form-item label="删除状态(0-正常,1-已删除)" prop="delFlag" :label-width="formLabelWidth">
          <el-input v-model="interface.delFlag" autocomplete="off" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" size="mini" @click="handleSaveOrUpdate()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { del, get, getPage, save, update } from '@/api/Interface'

export default {
  data() {
    return {
      total: null,
      list: null,
      listLoading: true,
      dialogVisible: false,
      listQuery: {
        page: 1,
        size: 10
      },
      interface: {
        businessId: null,
        systemId: null,
        menuId: null,
        interfaceId: null,
        interfaceName: null,
        userId: null,
        type: null,
        status: null,
        createId: null,
        createTime: null,
        updateId: null,
        updateTime: null,
        delFlag: null,
      },
      interfaceCopy: null,
      rules: {
        businessId: [{ required: true, message: '请填写企业ID', trigger: 'blur' }],
        systemId: [{ required: true, message: '请填写系统id', trigger: 'blur' }],
        menuId: [{ required: true, message: '请填写菜单ID', trigger: 'blur' }],
        interfaceId: [{ required: true, message: '请填写接口ID', trigger: 'blur' }],
        interfaceName: [{ required: true, message: '请填写接口名称', trigger: 'blur' }],
        userId: [{ required: true, message: '请填写创建用户ID', trigger: 'blur' }],
        type: [{ required: true, message: '请填写类型', trigger: 'blur' }],
        status: [{ required: true, message: '请填写状态', trigger: 'blur' }],
        createId: [{ required: true, message: '请填写创建人', trigger: 'blur' }],
        createTime: [{ required: true, message: '请填写创建时间', trigger: 'blur' }],
        updateId: [{ required: true, message: '请填写更新人', trigger: 'blur' }],
        updateTime: [{ required: true, message: '请填写更新时间', trigger: 'blur' }],
        delFlag: [{ required: true, message: '请填写删除状态(0-正常,1-已删除)', trigger: 'blur' }],
      },
      formLabelWidth: '120px'
    }
  },
  created() {
    this.getPage()
  },
  methods: {
    getPage() {
      this.listLoading = true
      getPage(this.listQuery).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleSizeChange(val) {
      this.listQuery.size = val
      this.getPage()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getPage()
    },
    indexMethod(index) {
      return this.listQuery.size * (this.listQuery.page - 1) + index + 1
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getPage()
    },
    handleCreate() {
      Object.keys(this.interface).forEach(key => (this.interface[key] = null))
      this.interfaceCopy = null
      this.dialogVisible = true
      if (this.$refs['interfaceForm'] !== undefined) {
        this.$refs['interfaceForm'].clearValidate()
      }
    },
    handleEdit(row) {
      const params = { id: row.interfaceId }
      get(params).then(response => {
        this.interface = response.data
        this.interfaceCopy = response.data
      })
      this.dialogVisible = true
      if (this.$refs['interfaceForm'] !== undefined) {
        this.$refs['interfaceForm'].clearValidate()
      }
    },
    handleDelete(row) {
      this.$confirm('是否删除该数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del(row.interfaceId).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功'
          })
          this.getPage()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '取消删除'
        })
      })
    },
    handleSaveOrUpdate() {
      this.$refs['interfaceForm'].validate((valid) => {
        if (valid) {
          if (this.interface.interfaceId) {
            update(this.interface).then(() => {
              this.$message({
                type: 'success',
                message: '更新成功'
              })
              this.dialogVisible = false
              this.getPage()
            })
          } else {
            save(this.interface).then(() => {
              this.$message({
                type: 'success',
                message: '保存成功'
              })
              this.dialogVisible = false
              this.getPage()
            })
          }
        } else {
          this.$message.error('请保证数据填写正确！')
        }
      })
    }
  }
}
</script>

<style>
</style>
